<?php

class Magentochina_Bundlepromotion_Model_Salerule extends Mage_SalesRule_Model_Rule
{
    private $_object;
	public function init( $object )
	{
		$this->_object = $object;
		$this->setData( 'name',$object->getData('title') );
		$this->setData( 'is_active','1' );
		$this->setData( 'is_advanced','1' );
		$this->setData( 'customer_group_ids',$object->getData('customer_group_ids') );
		$this->setData( 'simple_action',$object->getData('simple_action') );
		$this->setData( 'discount_amount',$object->getData('discount_amount') );
		$this->setData( 'is_rss',1 );
		$this->setData( 'website_ids',1 );
		$this->setData( 'from_date',date('Y/m/d',time()-172800) );
		$this->setData( 'conditions_serialized',$this->getConditionsSerialized() );
		$this->setData( 'actions_serialized',$this->getActionsSerialized() );
		
	
		return $this;
		
	}
	
	protected function _afterLoad()
    {
        parent::_afterLoad();       
        $websiteIds = $this->_getData('website_ids');
        if (is_string($websiteIds)) {
            $this->setWebsiteIds(explode(',', $websiteIds));
        }
        $groupIds = $this->getCustomerGroupIds();
        if (is_string($groupIds)) {
            $this->setCustomerGroupIds(explode(',', $groupIds));
        }
    }
	
	/**
     * Prepare data before saving
     *
     * @return Mage_Rule_Model_Rule
     */
    protected function _beforeSave()
    {
        if( $this->getData('rule_id') ){
			parent::_beforeSave();
		}
		// check if discount amount > 0
        if ((int)$this->getDiscountAmount() < 0) {
            Mage::throwException(Mage::helper('rule')->__('Invalid discount amount.'));
        }

        if (is_array($this->getCustomerGroupIds())) {
            $this->setCustomerGroupIds(join(',', $this->getCustomerGroupIds()));
        }        
    }

	
	protected function getConditionsSerialized()
	{
		$condition = array();
		$condition['type'] = 'salesrule/rule_condition_combine';
		$condition['attribute'] = '';
		$condition['operator'] = '';
		$condition['value'] = 1;
		$condition['is_value_processed'] = '';
		$condition['aggregator'] = 'all';
		$condition['conditions'] = array();
		
		$models = explode( ',',$this->_object->getData('models') );
		foreach( $models as $model ){
			$condition['conditions'][] = array( 
			'type' => 'salesrule/rule_condition_product_found',
			'attribute' => '',
			'operator' => '',
			'value' => 1,
			'is_value_processed' => '',
			'aggregator' => 'all',
			'conditions' =>array( array(
							'type' => 'salesrule/rule_condition_product',
							'attribute' => 'model',
							'operator' => '==',
							'value' => $model,
							'is_value_processed' => '',
						) ),
			);
			
		}	
		return serialize($condition);
	}
	
	protected function getActionsSerialized()
	{
		$condition = array();
		$condition['type'] = 'salesrule/rule_condition_combine';
		$condition['attribute'] = '';
		$condition['operator'] = '';
		$condition['value'] = 1;
		$condition['is_value_processed'] = '';
		$condition['aggregator'] = 'all';
		$condition['conditions'] = array();
		
		$condition['conditions'][] =  array(
								'type' => 'salesrule/rule_condition_product',
								'attribute' => 'model',
								'operator' => '()',
								'value' => $this->_object->getData('models'),
								'is_value_processed' => '',
							);
		
		return serialize($condition);
	}
}